package com.jeffgalper.feather;

import java.io.IOException;

public class FeatherApp {

  public static void main(String[] args) throws IOException, FeatherException {
    checkArgs(args);
    String featherPath = args[1];
    Feather fthrFile = new Feather(featherPath);
    if (args[0].equals("c")) {
      String[] files = new String[args.length - 2];
      System.arraycopy(args, 2, files, 0, args.length - 2);
      create(fthrFile, files);
    } else {
      extract(fthrFile);
    }
  }

  private static void extract(Feather fthrFile) throws IOException {
    while (fthrFile.hasFeathered()) {
      Feathered file = fthrFile.getFeathered();
      file.unfeather();
    }
  }

  private static void create(Feather fthrFile, String[] files)
      throws IOException, FeatherException {
    for (String filename : files) {
      fthrFile.addFile(filename);
    }
  }

  private static void checkArgs(String[] args) {
    if (args.length < 2) {
      usage();
    }
    if (!args[0].equals("c") && !args[0].equals("x")) {
      usage();
    }
    if (args[0].equals("c") && args.length < 3) {
      usage();
    }
  }

  private static void usage() {
    System.err.println("Usage: FeatherApp [cx] featherFile [ file ... ]");
    System.exit(1);
  }

}
